<?php

include_once ('lib/parvis.constants.php');
require_once ('lib/class.http.php');
require_once ('lib/dump.php');
require_once ('lib/class.urls.php');

// Retrouver la langue de l'URL
$langage = urls::getLangNum();
ob_start();
$api = new Http();
$api->debug = $debug;

//$url = 'http://parvis.ch/cgi-bin/cgiip.exe/WService=wsbroker1/compte.w';
$url = sprintf('%scompte.w', WEBSPEED);

$api->useCurl(false);

if (empty($_POST)) {
	// 1er appel: GET
    $api->setMethod('GET');
	$_GET['par-prog'] = 'commande';
    $api->addParam('lang', $langage);
	$api->addParam('langue', $langage);
    $api->addParam('par-prog', 'commande');
} else {
	// Appels suivants (via un des boutons de l'interface)
    $api->setMethod('POST');
	$_POST['par-prog'] = 'commande';
    // Les param�tres de POST sont redécodés en ISO
    foreach ($_POST as $key => $post) {
	    $param = utf8_decode($_POST[$key]);

	    $api->addParam($key, $param);
	}
}

$api->useCookie(true);
$api->saveCookie(true);

$api->addCookie('SessionId', $_COOKIE['SessionId']);
$api->addCookie('Livraison', $_COOKIE['Livraison']);
$api->addCookie('abonnement', $_COOKIE['abonnement']);
$api->addCookie('TempCli', $_COOKIE['TempCli']);

$api->execute($url);

$cookies = $api->_cookies;

if ($debug) {
	dump($api->params, 'POST Params: ');
	dump($cookies, 'Cookies retourné par le serveur');

	dump($cookies[0]['value'], 'value');
}


if (!empty($cookies[0])) {
	// Stockage "local" du cookie retourné par le serveur 
	if ($cookies[0]['name'] == 'SessionId') {
		setcookie('SessionId', $cookies[0]['value'], COOKIES_TIME, COOKIES_PATH);
	}
	if ($cookies[0]['name'] == 'Livraison') {
		setcookie('Livraison', $cookies[0]['value'], COOKIES_TIME, COOKIES_PATH);
	}
	if ($cookies[0]['name'] == 'abonnement') {
		setcookie('abonnement', $cookies[0]['value'], COOKIES_TIME, COOKIES_PATH);
	}	
	if ($cookies[0]['name'] == 'TempCli') {
		setcookie('TempCli', $cookies[0]['value'], COOKIES_TIME, COOKIES_PATH);
	}		
}


echo ($api->error) ? $api->error : utf8_encode($api->result);
$content = ob_get_contents();

ob_end_clean();
$action= strip_tags($content);
$action = preg_replace('/\s+/', '', $action);
$parts = explode("|",$action);
//echo"%$content%";

include('switch.php');
//dump($_GET);
//dump($_COOKIES);
?>
